package com.lifu.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lifu.yygh.common.result.Result;
import com.lifu.yygh.common.utils.MD5;
import com.lifu.yygh.hosp.service.HosptialSetService;
import com.lifu.yygh.model.hosp.HospitalSet;
import com.lifu.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Description
 * @Author lifu
 * @Date 2021/4/2 15:25
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping(value = "/admin/hosp/hosptialSet")
public class HosptialSetController {
    @Autowired
    private HosptialSetService hosptialSetService;
    //查询医院设置的所有信息
    @ApiOperation(value = "获取所有的医院设置信息")
    @GetMapping(value = "/findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> list = hosptialSetService.list();
        Result<List<HospitalSet>> ok = Result.ok(list);
        return ok;
    }
    //删除医院设置
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping(value = "/{id}")
    public Result removeHosptialSet(@PathVariable("id") Long id){
        boolean b = hosptialSetService.removeById(id);
        if(b == true){
            return Result.ok();
        }
        return Result.fail();
    }
    //条件查询带分页
    @PostMapping(value = "/findPage/{currnet}/{limit}")
    public Result findPageHospitalSet(@PathVariable("currnet") Long current,
                                      @PathVariable("limit") Long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        //判断这两个值是否存在再进行模糊查询
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hosname)){
            hospitalSetQueryWrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            hospitalSetQueryWrapper.like("hoscode", hoscode);
        }
        Page<HospitalSet> hospitalSetPage = hosptialSetService.page(page, hospitalSetQueryWrapper);
        return Result.ok(hospitalSetPage);
    }
    //添加医院设置
    @PostMapping(value = "/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean b = hosptialSetService.save(hospitalSet);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //根据id获取医院设置
    @GetMapping(value = "/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hosptialSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    //修改医院设置
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hosptialSetService.updateById(hospitalSet);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //批量删除医院设置
    @DeleteMapping(value = "/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean b = hosptialSetService.removeByIds(idList);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //医院设置锁定和解锁
    @PutMapping(value = "/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status){
        HospitalSet hospitalSet = hosptialSetService.getById(id);
        hospitalSet.setStatus(status);
        hosptialSetService.updateById(hospitalSet);
        return Result.ok();
    }
    //发送签名密钥
    @PutMapping(value = "sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hosptialSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
